﻿using System.Collections.Generic;
using Foxit.PDF;
using Foxit.PDF.PageElements;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Reporting.Framework;
using Sustainalytics.ESG.Entities;

namespace Sustainalytics.ReportGenerator.BL
{
    public class TearSheetOverviewSection : PdfReportSectionGenerator
    {
        private const float _sectionNameFontSize = 12f;

        public override void ComposeSectionContent()
        {
            const float analystCommentaryPosition = 110f;


            var page = _report.NewPage();
            _report.Pages.Add(page);



            AddHeaderToPage(page);


            var table = AddAnalystCommentary(analystCommentaryPosition);
            page.Elements.Add(table);

            table = AddSustainabilityBenchmark(analystCommentaryPosition);
            page.Elements.Add(table);

            var riskAssessmentPosition = 408f;
            table = AddSustainabilityProfile(riskAssessmentPosition);
            page.Elements.Add(table);

            table = AddControversyBencmark(riskAssessmentPosition);
            page.Elements.Add(table);

            
            var thematicAssesmentPosition = 610;

            table = AddThematicBenchmark(thematicAssesmentPosition);
            page.Elements.Add(table);


            var outline = new Reporting.Framework.OutlinePosition
            {
                Name = "Overview",
                PageNumber = 1,
                ChildOutlinePositions = new List<Reporting.Framework.OutlinePosition>
                {
                    new Reporting.Framework.OutlinePosition { Name = "Analyst Comentary", PageNumber = 1, VerticalPosition = analystCommentaryPosition },
                    new Reporting.Framework.OutlinePosition { Name = "ESG Profile", PageNumber = 1, VerticalPosition = riskAssessmentPosition },
                    new Reporting.Framework.OutlinePosition { Name = "Thematic Benchmark", PageNumber = 1, VerticalPosition = thematicAssesmentPosition },
               }
            };

            _outlinePositions.Add(outline);
        }

        private void AddHeaderToPage(Page page)
        {
            const float tableWidth = 320f;


            var companyName = $"<p><b>{_dataSource.CorporateData.CompanyName}</b></p>";
            const string helpText = "<p>This report measures the company's management of environmental, social and governance issues that are material to its business.</p>";

            var industry = string.IsNullOrEmpty(_dataSource.CorporateData.PeerGroup)
                ? "N/A"
                : _dataSource.CorporateData.PeerGroup;
            var domicile = string.IsNullOrEmpty(_dataSource.CorporateData.Country)
                ? "N/A"
                : _dataSource.CorporateData.Country;
            var employees = _dataSource.CorporateData.TotalEmployees == null
                ? "N/A"
                : $"{_dataSource.CorporateData.TotalEmployees:N0}";

            var companyInfo = $"<p>Industry: <b>{industry}</b>  Domicile: <b>{domicile}</b>  Employees: <b>{employees}</b></p>";



            var top = -20f;

            page.Elements.Add(new Rectangle(_report.LeftMargin, top, _report.PageMaxWidth, 15f, new RgbColor(252, 175, 23), new RgbColor(252, 175, 23)));




            var companyNameTextArea = new FormattedTextArea(companyName, 0f, 0f, tableWidth, 100f, Fonts.CalibriFontFamily, 16f, false);
            var companyNameHeight = (int)companyNameTextArea.GetRequiredHeight() + 5f;

            top += 35;
            if (companyNameHeight > 30)
            {
                top -= 15;
            }


            var helpTextArea = new FormattedTextArea(helpText, 0f, 0f, tableWidth - 130f, 100f, Fonts.CalibriFontFamily, 6.5f, false);
            var helpTextHeight = (int)helpTextArea.GetRequiredHeight() + 5f;


            var tableHeight = 20f + companyNameHeight + 15f + helpTextHeight + 10f;

            var table = GetDefaultTable(_report.LeftMargin, top, tableWidth, tableHeight);

            table.Columns.Add(tableWidth);

            var row = table.Rows.Add(20f);
            row.Cells.Add(new Label("Environmental, Social, and Governance (ESG) Report", 0f, 0f, tableWidth,
                11f + 2f, Fonts.CalibriBold, 11f, TextAlign.Left, RgbColor.Black));

            row = table.Rows.Add(companyNameHeight);
            row.Cells.Add(new FormattedTextArea(companyName, 0f, 0f, tableWidth, companyNameHeight, Fonts.CalibriFontFamily, 16f, false));

            row = table.Rows.Add(15f);
            row.Cells.Add(new FormattedTextArea(companyInfo, 0f, 0f, tableWidth, 15f, Fonts.CalibriFontFamily, 8f, false));

            row = table.Rows.Add(helpTextHeight);
            row.Cells.Add(new FormattedTextArea(helpText, 0f, 0f, tableWidth - 130f, helpTextHeight, Fonts.CalibriFontFamily, 6.5f, false));

            page.Elements.Add(table);

            var lastUpdate = $"Last annual report used: {_dataSource.RPCompanyProfile.LastAnnualReportDate.Year}";


            page.Elements.Add(new Label(lastUpdate, _report.LeftMargin + 180f, 88f, 140f,
                8f + 2f, Fonts.CalibriRegular, 8f, TextAlign.Right, RgbColor.Black));
            page.Elements.Add(new Label("Last modification made to the report: monthly update", _report.LeftMargin + 120f, 98f, 200f,
                8f + 2f, Fonts.CalibriRegular, 8f, TextAlign.Right, RgbColor.Black));

            tableHeight = 150f;

            const float firstColumnWidth = 125f;
            const float secondColumnWidth = 80f;

            var aTable = GetDefaultTable(_report.LeftMargin + 340f, -4f, firstColumnWidth + firstColumnWidth, tableHeight);
            aTable.CellDefault.VAlign = VAlign.Bottom;
            aTable.CellDefault.BackgroundColor = new RgbColor(255, 243, 220);

            aTable.Columns.Add(firstColumnWidth);
            aTable.Columns.Add(secondColumnWidth);

            var aRow = aTable.Rows.Add(10f);
            aRow.Cells.Add(string.Empty);
            aRow.Cells.Add(string.Empty);

            aRow = aTable.Rows.Add(20f);
            aRow.Cells.Add(
                new Label("ESG PERFORMANCE ASSESSMENT", 10f, 0f, firstColumnWidth + secondColumnWidth,
                    11f + 2f, Fonts.CalibriBold, 11f, TextAlign.Left, RgbColor.Black), 2);

            aRow = aTable.Rows.Add(30f);
            aRow.Cells.Add(
               new Label(_dataSource.OverallPerformance.CompanyAssessment.Name, 10f, 0f, firstColumnWidth,
                   12.5f + 2f, Fonts.CalibriBold, 12.5f, TextAlign.Left, new RgbColor(20, 167, 157)));
            aRow.Cells.Add(new RatingControl
            {
                Left = 0.0f,
                Top = 10.0f,
                Value = _dataSource.OverallPerformance.CompanyAssessment.Id
            });


            aRow = aTable.Rows.Add(25f);
            aRow.Cells.Add(
                new Label("OVERALL ESG SCORE", 10f, 0f, firstColumnWidth,
                    8f + 2f, Fonts.CalibriBold, 8f, TextAlign.Left, RgbColor.Black));
            aRow.Cells.Add(
                new Label("RELATIVE RANK", 0f, 0f, secondColumnWidth,
                    8f + 2f, Fonts.CalibriBold, 8f, TextAlign.Left, RgbColor.Black));

            aRow = aTable.Rows.Add(25f);
            aRow.Cells.Add(
                new FormattedTextArea($"<p>{_dataSource.OverallPerformance.CompanyScore} <font color='#000000' PointSize=10> out of 100</font></p>",
                    10f, 0f, secondColumnWidth, 18f + 2f, Fonts.CalibriLightFontFamily, 18f, false));
            aRow.Cells.Add(
                new FormattedTextArea($"<p>{_dataSource.OverallPerformance.CompanyRank} <font color='#000000' PointSize=10> out of {_dataSource.OverallPerformance.CompanyCount}</font></p>",
                    0f, 0f, firstColumnWidth, 18f + 2f, Fonts.CalibriLightFontFamily, 18f, false));

            aRow = aTable.Rows.Add(10f);
            aRow.Cells.Add(string.Empty);
            aRow.Cells.Add(string.Empty);


            page.Elements.Add(aTable);
        }

        private Table AddAnalystCommentary(float tableTop)
        {
            const float tableWidth = 320f;
            const float emptyRowHeight = 5f;

            const float sectionTitleHeight = 20f;
            const string sectionName = "<p><b>ANALYST COMMENTARY</b></p>";


            var style = new FormattedTextAreaStyle(Fonts.CalibriFontFamily, 7.5f, false)
            {
                Paragraph = { Align = TextAlign.Justify, Indent = 0, SpacingAfter = 5, AllowOrphanLines = false }
            };

            var analystCommentary = string.Empty;
            var rowHeight = 5f;
            FormattedTextArea formattedTextArea;

            if (_dataSource.RiskRatingResearchProfile != null)
            {
                analystCommentary = string.IsNullOrEmpty(_dataSource.RiskRatingResearchProfile.EsgRiskManagementComment.Value)
                ? string.Empty
                : _dataSource.RiskRatingResearchProfile.EsgRiskManagementComment.Value;

                formattedTextArea = new FormattedTextArea(analystCommentary, 0.0f, 0.0f, tableWidth, 500f, style);
                rowHeight = formattedTextArea.GetRequiredHeight() + 5f;
            }


            var tableHeight = emptyRowHeight + sectionTitleHeight + rowHeight + 20f;

            var table = GetDefaultTable(_report.LeftMargin, tableTop, tableWidth, tableHeight);

            table.Columns.Add(tableWidth);

            var row = table.Rows.Add(emptyRowHeight);
            var cell = row.Cells.Add(string.Empty);
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Top.Color = RgbColor.Black;

            row = table.Rows.Add(sectionTitleHeight);
            row.Cells.Add(new FormattedTextArea(sectionName, 0f, 0f, tableWidth, _sectionNameFontSize + 2f, Fonts.CalibriFontFamily, _sectionNameFontSize, false));


            formattedTextArea = new FormattedTextArea(analystCommentary, 0.0f, 0.0f, tableWidth, rowHeight, style);

            row = table.Rows.Add(rowHeight);
            row.Cells.Add(formattedTextArea);

            return table;
        }

        private Table AddSustainabilityBenchmark(float tableTop)
        {
            const float tableWidth = 205f;
            const float emptyRowHeight = 5f;
            const float chartHeight = 120f;

            const float sectionTitleHeight = 20f;
            const string sectionName = "<p><b>INDUSTRY ESG BENCHMARK</b></p>";


            var style = new FormattedTextAreaStyle(Fonts.CalibriFontFamily, 6.5f, false)
            {
                Paragraph = { Align = TextAlign.Justify, Indent = 0, SpacingAfter = 5, AllowOrphanLines = false }
            };

            const string helpText = "<p><font color='#000000'>The graph below indicates the percentage of companies in the global industry peer group that fall within each sustainability assessment category.</font></p>";

            var formattedTextArea = new FormattedTextArea(helpText, 0.0f, 0.0f, tableWidth, 50f, style);
            var rowHeight = formattedTextArea.GetRequiredHeight() + 5f;



            var tableHeight = emptyRowHeight + sectionTitleHeight + rowHeight + chartHeight + 20f;

            var table = GetDefaultTable(_report.LeftMargin + 340f, tableTop, tableWidth, tableHeight);

            table.Columns.Add(tableWidth);

            var row = table.Rows.Add(emptyRowHeight);
            var cell = row.Cells.Add(string.Empty);
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Top.Color = RgbColor.Black;

            row = table.Rows.Add(sectionTitleHeight);
            row.Cells.Add(new FormattedTextArea(sectionName, 0f, 0f, tableWidth, _sectionNameFontSize + 2f, Fonts.CalibriFontFamily, _sectionNameFontSize, false));

            formattedTextArea = new FormattedTextArea(helpText, 0.0f, 0.0f, tableWidth, rowHeight, style);
           
            row = table.Rows.Add(chartHeight);
            row.Cells.Add(new BenchmarkChart(0f, 5f, _dataSource.OverallPerformance.CompanyAssessment.Id, _dataSource.SustainabilityAssessments, true));

            return table;
          
        }

        private Table AddSustainabilityProfile(float tableTop)
        {
            
            const float tableWidth = 320f;
            const float emptyRowHeight = 10f;

            const float sectionTitleHeight = 20f;
            const string sectionName = "<p><b>COMPANY ESG RISK PROFILE</b></p>";



            var style = new FormattedTextAreaStyle(Fonts.CalibriFontFamily, 6.5f, false)
            {
                Paragraph = { Align = TextAlign.Justify, Indent = 0, SpacingAfter = 5, AllowOrphanLines = false }
            };

            const string helpText = "<p><font color='#000000'>The graph below compares a company's sustainability management to its involvement in controversies in order to provide an assessment of the company's sustainability risk profile.</font></p>";



            var formattedTextArea = new FormattedTextArea(helpText, 0.0f, 0.0f, tableWidth, 50f, style);
            var rowHeight = formattedTextArea.GetRequiredHeight() + 5f;

            var chart = SustainabilityProfileChart();
            var chartHeight = chart.GetRequiredHeight();

            var tableHeight = emptyRowHeight + sectionTitleHeight + rowHeight + chartHeight+ 10f;

            var table = GetDefaultTable(_report.LeftMargin, tableTop, tableWidth, tableHeight);

            table.Columns.Add(tableWidth);

            var row = table.Rows.Add(emptyRowHeight);
            var cell = row.Cells.Add(string.Empty);
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Top.Color = RgbColor.Black;

            row = table.Rows.Add(sectionTitleHeight);
            row.Cells.Add(new FormattedTextArea(sectionName, 0f, 0f, tableWidth, _sectionNameFontSize + 2f, Fonts.CalibriFontFamily, _sectionNameFontSize, false));

            row = table.Rows.Add(rowHeight);
            row.Cells.Add(new FormattedTextArea(helpText, 0.0f, 0.0f, tableWidth, rowHeight, style));
           
            row = table.Rows.Add(chartHeight);
            row.Cells.Add(chart);

            return table;
        }

        private Group ProfileChartLegend()
        {
            var elements = new Group();
            var left = 125f;
            var top = 3f;
            var lowColor = new RgbColor(228, 240, 240);
            var mediumColor = new RgbColor(173, 215, 211);
            var highColor = new RgbColor(20, 167, 157);

            elements.Add(new Label("Risk Level", left, top, 50f,
                    7f + 2f, Fonts.CalibriRegular, 7f, TextAlign.Left, RgbColor.Black));
            left += 40f;

            elements.Add(new Rectangle(left, top + 1f, 5f, 5f, lowColor, lowColor));
            left += 10f;
            elements.Add(new Label("Low", left, top, 50f,
                    7f + 2f, Fonts.CalibriRegular, 7f, TextAlign.Left, RgbColor.Black));
            left += 25f;

            elements.Add(new Rectangle(left, top + 1f, 5f, 5f, mediumColor, mediumColor));
            left += 10f;
            elements.Add(new Label("Medium", left, top, 50f,
                    7f + 2f, Fonts.CalibriRegular, 7f, TextAlign.Left, RgbColor.Black));
            left += 35f;

            elements.Add(new Rectangle(left, top + 1f, 5f, 5f, highColor, highColor));
            left += 10f;
            elements.Add(new Label("High", left, top, 50f,
                    7f + 2f, Fonts.CalibriRegular, 7f, TextAlign.Left, RgbColor.Black));


            return elements;
        }

        private Table SustainabilityProfileChart()
        {

            const float innerTableWidth = 300f;
            const float rowHeight = 15.5f;
            const float tableHeight = 5f + 6 * rowHeight;
            const float columnWidth = 36f;
            const float titleFontSize = 8f;
            const float labelSize = 7f;
            const float firstColumnWidth = 74f;

            var sustainabilityAssessments = GetSustainabilityAssessments();
            var controversyAssessments = GetControversyAssessments();


            const float bulletRadius = 2f;

            var table = GetDefaultTable(0f, -15f, innerTableWidth, tableHeight + 15f);
            table.CellDefault.Border.Width = 1f;
            table.CellDefault.Border.LineStyle = LineStyle.Solid;
            table.CellDefault.Border.Color = RgbColor.White;
            table.CellDefault.BackgroundColor = RgbColor.White;


            table.Columns.Add(firstColumnWidth);
            table.Columns.Add(columnWidth);
            table.Columns.Add(columnWidth);
            table.Columns.Add(columnWidth);
            table.Columns.Add(columnWidth);
            table.Columns.Add(columnWidth);
            table.Columns.Add(columnWidth);


            var row = table.Rows.Add(5f);
            var cell = row.Cells.Add(string.Empty, 7);
            cell.BackgroundColor = RgbColor.White;


            for (var sIndex = 0; sIndex < 5; sIndex++)
            {
                var font = Fonts.CalibriRegular;
                var color = RgbColor.Black;

                if (_dataSource.OverallPerformance.CompanyAssessment.Id == sustainabilityAssessments[sIndex].Id)
                {
                    font = Fonts.CalibriBold;
                    color = new RgbColor(20, 167, 157);
                }

                row = table.Rows.Add(rowHeight);
                row.CellDefault.VAlign = VAlign.Center;


                row.Cells.Add(new Label(sustainabilityAssessments[sIndex].Name, 0f, 0f, 65f,
                labelSize + 2f, font, labelSize, TextAlign.Right, color));

                for (var cIndex = 0; cIndex < 6; cIndex++)
                {
                    if (_dataSource.OverallPerformance.CompanyAssessment.Id == sustainabilityAssessments[sIndex].Id &&
                        _dataSource.HighestControversyLevel == controversyAssessments[cIndex].Id)
                    {
                        cell = row.Cells.Add(
                            new Circle(columnWidth / 2 - bulletRadius / 2, rowHeight / 2 - bulletRadius / 2, bulletRadius,
                                RgbColor.Black, RgbColor.Black, bulletRadius / 2, LineStyle.Solid));
                        cell.BackgroundColor = GetCellBackgroundColor(sustainabilityAssessments[sIndex].Id, cIndex);
                    }
                    else
                    {
                        cell = row.Cells.Add(string.Empty);
                        cell.BackgroundColor = GetCellBackgroundColor(sustainabilityAssessments[sIndex].Id, cIndex);
                    }
                }
            }

            row = table.Rows.Add(rowHeight);
            row.CellDefault.Align = TextAlign.Center;
            row.CellDefault.BackgroundColor = RgbColor.White;

            row.Cells.Add(string.Empty);

            for (var index = 0; index < 6; index++)
            {
                var font = Fonts.CalibriRegular;
                var color = RgbColor.Black;

                if (_dataSource.HighestControversyLevel == controversyAssessments[index].Id)
                {
                    font = Fonts.CalibriBold;
                    color = new RgbColor(20, 167, 157);
                }

                cell = row.Cells.Add(new Label(controversyAssessments[index].Name, 0f, 0f, columnWidth+20f,
                    labelSize + 2f, font, labelSize, TextAlign.Center, color));
                cell.VAlign = VAlign.Bottom;
            }

            var innerTableHeight = (int)table.GetRequiredHeight();


            var mainTable = GetDefaultTable(0f, 0f, innerTableWidth + 20f, innerTableHeight + 45f + 10f);

            mainTable.Columns.Add(20f);
            mainTable.Columns.Add(innerTableWidth);

            var mainRow = mainTable.Rows.Add(25f);
            mainRow.Cells.Add(ProfileChartLegend(), 2);

            mainRow = mainTable.Rows.Add(innerTableHeight + 5f);
            mainRow.Cells.Add(new Label("ESG PERFORMANCE", 0f, innerTableHeight, innerTableHeight+20f,
               titleFontSize + 2f, Fonts.CalibriRegular, titleFontSize, TextAlign.Center, RgbColor.Black)
            { Angle = -90 });
            mainRow.Cells.Add(table);

            mainRow = mainTable.Rows.Add(20f);
            mainRow.CellDefault.VAlign = VAlign.Bottom;
            mainRow.Cells.Add(new Label("CONTROVERSIES", firstColumnWidth + 20f, -25f, innerTableWidth - firstColumnWidth,//era -7f
               titleFontSize + 2f, Fonts.CalibriRegular, titleFontSize, TextAlign.Center, RgbColor.Black), 2);

            return mainTable;
        }

        private RgbColor GetCellBackgroundColor(int sustainabilityAssessmentId, int controversyAssessmentId)
        {
            var lowColor = new RgbColor(228, 240, 240);
            var mediumColor = new RgbColor(173, 215, 211);
            var highColor = new RgbColor(20, 167, 157);

            if (sustainabilityAssessmentId > 0 && sustainabilityAssessmentId < 5 && controversyAssessmentId == 0)
                return lowColor;
            if (sustainabilityAssessmentId == 5 && controversyAssessmentId == 0)
                return mediumColor;

            if (sustainabilityAssessmentId > 0 && sustainabilityAssessmentId < 3 && (controversyAssessmentId == 1 || controversyAssessmentId == 2))
                return lowColor;
            if (sustainabilityAssessmentId > 2 && sustainabilityAssessmentId < 5 && (controversyAssessmentId == 1 || controversyAssessmentId == 2))
                return mediumColor;
            if (sustainabilityAssessmentId == 5 && (controversyAssessmentId == 1 || controversyAssessmentId == 2))
                return highColor;

            if (sustainabilityAssessmentId == 1 && controversyAssessmentId == 3)
                return lowColor;
            if (sustainabilityAssessmentId > 1 && sustainabilityAssessmentId < 5 && controversyAssessmentId == 3)
                return mediumColor;
            if (sustainabilityAssessmentId == 5 && controversyAssessmentId == 3)
                return highColor;

            if (sustainabilityAssessmentId == 1 && (controversyAssessmentId == 4 || controversyAssessmentId == 5))
                return lowColor;
            if (sustainabilityAssessmentId == 2 && (controversyAssessmentId == 4 || controversyAssessmentId == 5))
                return mediumColor;
            if (sustainabilityAssessmentId > 2 && (controversyAssessmentId == 4 || controversyAssessmentId == 5))
                return highColor;

            return RgbColor.Silver;
        }

        private static RPAssessment[] GetSustainabilityAssessments()
        {
            var assesments = new List<RPAssessment>()
            {
                new RPAssessment()
                {
                    Id = 5,
                    Name = "Laggard"
                },
                new RPAssessment()
                {
                    Id = 4,
                    Name = "Underperformer"
                },
                new RPAssessment()
                {
                    Id = 3,
                    Name = "Average Performer"
                },
                new RPAssessment()
                {
                    Id = 2,
                    Name = "Outperformer"
                },
                new RPAssessment()
                {
                    Id = 1,
                    Name = "Leader"
                }
            };

            return assesments.ToArray();
        }

        private static RPAssessment[] GetControversyAssessments()
        {
            var assesments = new List<RPAssessment>()
            {
                new RPAssessment()
                {
                    Id = 0,
                    Name = "None"
                },
                new RPAssessment()
                {
                    Id = 1,
                    Name = "Low"
                },
                new RPAssessment()
                {
                    Id = 2,
                    Name = "Moderate"
                },
                new RPAssessment()
                {
                    Id = 3,
                    Name = "Significant"
                },
                new RPAssessment()
                {
                    Id = 4,
                    Name = "High"
                },
                new RPAssessment()
                {
                    Id = 5,
                    Name = "Severe"
                }
            };

            return assesments.ToArray();
        }

        private Table AddControversyBencmark(float tableTop)
        {
            const float tableWidth = 205f;
            const float emptyRowHeight = 5f;
            const float chartHeight = 100f;

            const float sectionTitleHeight = 20f;
            const string sectionName = "<p><b>CONTROVERSY BENCHMARK</b></p>";

            var style = new FormattedTextAreaStyle(Fonts.CalibriFontFamily, 6.5f, false)
            {
                Paragraph = { Align = TextAlign.Justify, Indent = 0, SpacingAfter = 5, AllowOrphanLines = false }
            };

            const string helpText = "<p><font color='#000000'>The graph below indicates the percentage of companies in the global industry peer group that fall within each controversy assessment category.</font></p>";

            var formattedTextArea = new FormattedTextArea(helpText, 0.0f, 0.0f, tableWidth, 50f, style);
            var rowHeight = formattedTextArea.GetRequiredHeight() + 5f;

            var tableHeight = emptyRowHeight + sectionTitleHeight + rowHeight + chartHeight + 20f;
            var table = GetDefaultTable(_report.LeftMargin + 340f, tableTop, tableWidth, tableHeight);
           

            table.Columns.Add(tableWidth);

            var row = table.Rows.Add(emptyRowHeight);
            var cell = row.Cells.Add(string.Empty);
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Top.Color = RgbColor.Black;

            row = table.Rows.Add(sectionTitleHeight);
            row.Cells.Add(new FormattedTextArea(sectionName, 0f, 0f, tableWidth, _sectionNameFontSize + 2f, Fonts.CalibriFontFamily, _sectionNameFontSize, false));
            formattedTextArea = new FormattedTextArea(helpText, 0.0f, 0.0f, tableWidth, rowHeight, style);

            row = table.Rows.Add(rowHeight);
            row.Cells.Add(formattedTextArea);
            row = table.Rows.Add(chartHeight);
            row.Cells.Add(new BenchmarkChart(0f, 5f, _dataSource.HighestControversyLevel, _dataSource.ControversyAssessments, false));
            return table;
        }

        private Table AddThematicBenchmark(float tableTop)
        {
            const float emptyRowHeight = 5f;
            const float chartHeight = 120f;

            var tableWidth = _report.PageMaxWidth;
            var tableHeight = emptyRowHeight + chartHeight + 10f;

            var table = GetDefaultTable(_report.LeftMargin, tableTop, tableWidth, tableHeight);

            table.Columns.Add(tableWidth);

            var row = table.Rows.Add(emptyRowHeight);
            var cell = row.Cells.Add(string.Empty, 2);
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Top.Color = RgbColor.Black;

            var themeScores = new[]
            {
                _dataSource.OverallPerformance.EnvironmentScore,
                _dataSource.OverallPerformance.SocialScore,
                _dataSource.OverallPerformance.GovernanceScore
            };

            var companyThemeAssesments = new[]
            {
                _dataSource.OverallPerformance.EnvironmentAssessment.Id,
                _dataSource.OverallPerformance.SocialAssessment.Id,
                _dataSource.OverallPerformance.GovernanceAssessment.Id
            };

            row = table.Rows.Add(chartHeight);
            row.Cells.Add(new ThematicBenchmarkChart(0f, 0f, themeScores, companyThemeAssesments, _dataSource.ThematicAssessments), 2);


            return table;
        }
    }
}
